package service;/*
@Date:2024/12/4
*/

import dao.Dao;
import javabean.House;
import tool.Utility;

import java.sql.*;
import java.util.List;
import java.util.Scanner;

public class HouseService {
    //final ArrayList<javabean.House[]> object=  new ArrayList<>();
    final Dao dao = new Dao();
    House[] houses = new House[10];

    public HouseService() {
        for (int i = 0; i < houses.length; i++) {
            houses[i] = new House();
            houses[i].setId(String.valueOf(i));
        }
    }

    //返回房屋信息
    public void list() {
//            System.out.println("-----房屋列表-----");
//            for (int i = 0; i < houses.length; i++) {
//                    System.out.println(houses[i]);
//            }
        final List<House> houses = dao.find();
        System.out.println("---------------房屋列表---------------");
        System.out.println("编号\t\t房主\t\t电话\t\t地址\t\t月租\t\t状态(未出租/已出租)");
        for (int i = 0; i < houses.size(); i++) {
            System.out.println(houses.get(i).getId() + "\t\t" + houses.get(i).getName() + "\t\t" + houses.get(i).getTel() + "\t\t" + houses.get(i).getAddress() + "\t\t" + houses.get(i).getMoney()
                    + "\t\t" + houses.get(i).getState());
        }
        System.out.println("---------------房屋列表完成---------------");
    }


    //添加
    public boolean add() {
        System.out.println("-----添加房屋-----");
        System.out.println("房源id");
        int x = Utility.readInt();
        //houses[x] = new javabean.House();
        if (x < 0 || x >= houses.length) {
            System.out.println("无效的房源ID");
            return false;
        }
        if (houses[x].getState().equals("已出租")) {
            System.out.println("该房源已出租");
            return false;
        }
        System.out.println("姓名");
        this.houses[x].setName(Utility.readKeyBoard(4, false));
        System.out.println("电话");
        houses[x].setTel(Utility.readString(11));
        System.out.println("地址");
        houses[x].setAddress(Utility.readKeyBoard(20, false));
        System.out.println("月租");
        houses[x].setMoney(Utility.readKeyBoard(20, false));
        houses[x].setState("已出租");
        //object.add(houses);
        final int up = dao.in(houses[x]);
        return true;
    }

    //删除
    public boolean del() {
        System.out.println("-----删除房屋-----");
        System.out.println("请输入删除房源id");
        int x = Utility.readInt();
        System.out.println("请确认是否删除(Y/N)请小心选择:");
        char c = Utility.readConfirmSelection();
        if (c == 'Y') {
            //houses[x] = null;
//                for (int i = 0; i < houses.length; i++) {
//                    final javabean.House house = new javabean.House();
//                   house.setId(String.valueOf(x));
//                    houses[x]=house;
            //         }
            final int del = dao.del(x);
            if (del == 0) {
                System.out.println("删除失败");
                return false;
            }

            System.out.println("删除成功");
        }
        return true;
    }

    //查找
    public String findByld() {
        System.out.println("-----查找房屋-----");
        System.out.println("请输入查找房源id");
        int x = Utility.readInt();
//            if (houses[x].getState().equals("未出租")){
//                return null;
//            }
        List<House> housess = dao.findone(x);
        System.out.println("---------------房屋列表---------------");
        System.out.println("编号\t\t房主\t\t电话\t\t地址\t\t月租\t\t状态(未出租/已出租)");
        for (int i = 0; i < housess.size(); i++) {
            System.out.println(housess.get(i).getId() + "\t\t" + housess.get(i).getName() + "\t\t" + housess.get(i).getTel() + "\t\t" + housess.get(i).getAddress() + "\t\t" + housess.get(i).getMoney()
                    + "\t\t" + housess.get(i).getState());
        }
        return houses[x].toString();

    }

    //修改
    public boolean chang() {
        System.out.println("请输入修改房源id");
        int x = Utility.readInt();
        if (houses[x].getId().equals("null"))
            return false;

        System.out.println("请输入修改姓名");
        String z = Utility.readKeyBoard(4, false);
        houses[x].setName(z);
        System.out.println("请输入修改电话");
        houses[x].setTel(Utility.readString(11));
        System.out.println("请输入修改地址");
        houses[x].setAddress(Utility.readKeyBoard(20, false));
        System.out.println("请输入修改月租");
        houses[x].setMoney(Utility.readKeyBoard(20, false));
        houses[x].setState("已出租");
        final int Update = dao.Update(houses[x]);
        return true;
    }
    //登录
    public boolean isUser() throws SQLException {
        final boolean flag = dao.login();
        if(flag){
            if(iscap()) {
                System.out.println("登陆成功！");
                return true;
            }
            return false;
        }else {
            System.out.println("非法管理员！");
            return false;
        }
    }
    //验证码
    public boolean iscap() {
        final String s = dao.generateCode();
        System.out.println("请输入验证码   " + s);
        final Scanner scanner = new Scanner(System.in);
        final String s1 = scanner.nextLine();
        if (s.equals(s1)) {
            return true;
        } else
            return false;
    }

}
